package com.iespring.service.app.footprint;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.user.entity.MallFootprint;
import com.iespring.bean.admin.user.entity.MallUser;
import com.iespring.bean.app.footprint.vo.WxFootprintVo;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.mapper.admin.MallFootprintMapper;
import com.iespring.util.UserInfoUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Auth: 杨宇健
 * Time: 2023/1/1 16:19
 * Description:
 **/
@Service
public class WxFootPrintServiceImpl implements WxFootPrintService {
    @Resource
    MallFootprintMapper footprintMapper;

    @Override
    public BaseRespVo list(BasePageInfo pageInfo) {
        MallUser user = UserInfoUtils.getUser();
        if (user == null || user.getId() == null) {
            return BaseRespVo.error("请重新登录", 500);
        }
        pageInfo.setUserId(user.getId() + "");

        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());
        List<WxFootprintVo> footprints = footprintMapper.selectFootprintLimitWx(pageInfo);
        PageInfo pageInfoData = new PageInfo(footprints);
        CommonData data = CommonData.data(pageInfoData);
        return BaseRespVo.ok(data);
    }

    @Override
    public BaseRespVo delete(Integer id) {

        MallUser user = UserInfoUtils.getUser();

        MallFootprint mallFootprint = footprintMapper.selectByPrimaryKey(id);

        if (mallFootprint.getUserId() != user.getId()) {
            return BaseRespVo.error("不允许删除别人的足迹", 501);
        }

        mallFootprint.setDeleted(true);

        int affect = footprintMapper.updateByPrimaryKey(mallFootprint);

        if (affect == 0) {
            return BaseRespVo.error("删除失败", 500);
        }

        return BaseRespVo.ok();
    }

    @Override
    public BaseRespVo insert(Integer goodsId) {
        MallFootprint mallFootprint = new MallFootprint();
        mallFootprint.setGoodsId(goodsId);
        Date date = new Date();
        mallFootprint.setAddTime(date);
        mallFootprint.setUpdateTime(date);
        mallFootprint.setDeleted(false);
        mallFootprint.setUserId(UserInfoUtils.getUser().getId());
        footprintMapper.insert(mallFootprint);
        return BaseRespVo.ok();


    }
}
